<template>
	<xn-form-container
		:title="formData.id ? '编辑水表信息管理' : '增加水表信息管理'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="水表编号：" name="metersId">
						<a-input v-model:value="formData.metersId" placeholder="请输入水表编号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="水表名称：" name="metersName">
						<a-input v-model:value="formData.metersName" placeholder="请输入水表名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="水表型号：" name="metersTypeid">
						<a-input v-model:value="formData.metersTypeid" placeholder="请输入水表型号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="组装日期：" name="timeAssembly">
						<a-date-picker v-model:value="formData.timeAssembly" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择组装日期" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="组装人员：" name="perAssembly">
						<a-input v-model:value="formData.perAssembly" placeholder="请输入组装人员" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="安装日期：" name="installTime">
						<a-date-picker v-model:value="formData.installTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择安装日期" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="安装地点：" name="installLocal">
						<a-input v-model:value="formData.installLocal" placeholder="请输入安装地点" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="安装人员：" name="perInstall">
						<a-input v-model:value="formData.perInstall" placeholder="请输入安装人员" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="程序录入时间：" name="timeInput">
						<a-date-picker v-model:value="formData.timeInput" value-format="YYYY-MM-DD HH:mm:ss" show-time
									   placeholder="请选择程序录入时间" style="width: 100%"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="程序录入人员：" name="perInput">
						<a-input v-model:value="formData.perInput" placeholder="请输入程序录入人员" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="维修时间：" name="timeRepair">
						<a-date-picker v-model:value="formData.timeRepair" value-format="YYYY-MM-DD HH:mm:ss" show-time
									   placeholder="请选择维修时间" style="width: 100%"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="维修人员：" name="perRepair">
						<a-input v-model:value="formData.perRepair" placeholder="请输入维修人员" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="报废日期：" name="timeDamage">
						<a-date-picker v-model:value="formData.timeDamage" value-format="YYYY-MM-DD HH:mm:ss" show-time
									   placeholder="请选择报废日期" style="width: 100%"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="报废人员：" name="perDamage">
						<a-input v-model:value="formData.perDamage" placeholder="请输入报废人员" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="使用状态：" name="status">
						<a-select v-model:value="formData.status"
								  placeholder="请选择使用状态"
								  :options="statusOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="可选排序字段：" name="metersOrder">
						<a-input v-model:value="formData.metersOrder" placeholder="请输入序号" allow-clear/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="metersInfoForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import metersInfoApi from '@/api/productmanagement/metersInfoApi'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const statusOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	statusOptions.value = tool.dictList('status_type')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {
	metersId: [required('请输入水表编号')],
	metersName: [required('请输入水表名称')],
	metersTypeid: [required('请输入水表型号')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		metersInfoApi
			.metersInfoSubmitForm(formDataParam, formDataParam.id)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
